package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo24 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int num = 0;
                for (int i = 1; i <= 1000 ; i++) {
                    num+=i;
                }
                Thread.sleep(2000);
                return num;
            }
        };
        //此时完成包装
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();
        System.out.println(futureTask.get());
        //此处的get获取的上述call方法的返回值
        //get和join类似会阻塞等待,如果call没执行完就会阻塞等待
    }
}
